# Maintainer: Kentaro Hayashi <hayashi@clear-code.com>

_realname=mecab
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.996
pkgrel=6
pkgdesc="Yet another Japanese morphological analyzer (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://taku910.github.io/mecab/"
license=('spdx:GPL-2.0-or-later AND LGPL-2.1-or-later AND BSD-3-Clause-Clear') # licensed under triple lincense
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libiconv")
install=${_realname}-${MSYSTEM}.install
source=("https://deb.debian.org/debian/pool/main/m/mecab/mecab_0.996.orig.tar.gz"
        "mecab-0.996.diff"
        "mecab-0.996-iconv.patch"
        "mecab-0.996-naist-jdic.patch"
        "0001-dont-ignore-flags.patch"
        "0002-fix-stdcall.patch")
sha256sums=('e073325783135b72e666145c781bb48fada583d5224fb2490fb6c1403ba69c59'
            '88a56efae57e30ffcc1b117fe152f37dd7bc419368b1d0e422d5b62245dd2d5c'
            '2dff2b3ae7faec432885a804210d55e2ed68b1af03c138e8297e0c510ef48bdf'
            '222cc3938da2e9cd79908ceeda15889f4fe78664fb61ddb83a2a18f9809c4c6b'
            'c058e0bffe30ec2edc922a33535e113541f39ece04939ab0e1d08d67f4453b4a'
            'da8c59805a66549759dde8fb6241ecee1b43a73b0b99269be68bb688203f63ea')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/mecab-0.996.diff
  patch -p1 -i "${srcdir}"/mecab-0.996-iconv.patch
  patch -p1 -i "${srcdir}"/mecab-0.996-naist-jdic.patch

  patch -p1 -i "${srcdir}"/0001-dont-ignore-flags.patch
  patch -p1 -i "${srcdir}"/0002-fix-stdcall.patch

  sed -i.orig -e '/^AC_CHECK_LIB(stdc++/d' configure.in
  mkdir -p m4
  autoreconf --force
  libtoolize --install
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  CXXFLAGS+=" -std=c++11"

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST}

  make
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  make DESTDIR="${pkgdir}" install

  mv "${pkgdir}${MINGW_PREFIX}/etc/mecabrc" "${pkgdir}${MINGW_PREFIX}/bin/mecabrc"
  for license in AUTHORS BSD COPYING GPL LGPL; do
    install -Dm644 "${srcdir}"/${_realname}-${pkgver}/${license} "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/${license}"
  done
}
